package com.example.job;

import com.example.domain.entity.Plant;
import com.example.service.IPlantService;
import com.example.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.HashMap;
import java.util.Map;

/**
 * 更新数据库的关注量
 *
 * @author LYN
 * @date 2022/12/02
 */
@Component
public class UpdateFollowerNumJob {
    @Autowired
    RedisCache redisCache;

    @Autowired
    IPlantService plantService;

    @Scheduled(cron = "* 0/10 * * * ?")
    public void updateFollowerNumOfPlant(){
        Map<String,String> PlantFollowerNumMap=redisCache.getCacheMap("PlantFollowerNum");

        for(String plantId:PlantFollowerNumMap.keySet()){
            Plant plant=plantService.getById(Long.valueOf(plantId));
            if(plant==null){
                redisCache.deleteMapEntry("PlantFollowerNum",plantId);
            }else{
                plant.setFollowerNum(Long.valueOf(PlantFollowerNumMap.get(plantId)) );
                plantService.updateById(plant);
            }
        }
    }

}
